﻿
/*
 * @Author: wss 
 * @Date: 2020-10-21 15:54:58 
 * @Last Modified by: wss
 * @Last Modified time: 2020-10-21 16:08:46
 */
using System;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// UIAmountLerpBar - 血条组件，血条会根据数值变化，有不同的层级
/// ver 1.0 -  基本组件功能 - 2020-10-21 15:57:59 
/// </summary>
public class UISliceLerpBar : MonoBehaviour
{
    [Header("===== 图片 =====")]
    public Image mBgBar;
    private Image mImageBar1;
    private Image mImageBar2;
    public UISlicedProgressBar mBar1;
    public UISlicedProgressBar mBar2;


    [Header("===== 进度条的 颜色 =====")]
    public Color decreaseColor = Color.white;
    public Color increseColor = Color.white;
    public Color baseColor = Color.white;

    [Header("===== 开始时是否填充条 =====")]
    public bool startFillAnimate = false;

    [Range(0, 1)]
    public float progress = 1.0f;
    private float _progress = 0;
    public float lerpSpeed = 2f;

    private void Awake()
    {
        mImageBar1 = this.mBar1.GetComponent<Image>();
        mImageBar2 = this.mBar2.GetComponent<Image>();

        if (!startFillAnimate)
        {
            ForceSetProgress(1);
        }

    }

    private void OnValidate()
    {
        if (mImageBar2 == null) mImageBar2 = this.mBar2.GetComponent<Image>();
        mImageBar2.color = baseColor;
        ForceSetProgress(this.progress);
    }

    public void ForceSetProgress(float value)
    {
        _progress = progress = value;
        this.mBar2.progress = _progress;
        this.mBar1.progress = progress;
    }
    // Update is called once per frame
    void Update()
    {
        _progress = Mathf.Lerp(_progress, progress, lerpSpeed * Time.deltaTime);

        if (_progress < progress)
        {
            mImageBar1.color = increseColor;
            mImageBar2.color = baseColor;
            this.mBar2.progress = _progress;
            this.mBar1.progress = progress;
        }
        else
        {
            mImageBar1.color = decreaseColor;
            mImageBar2.color = baseColor;
            this.mBar1.progress = _progress;
            this.mBar2.progress = progress;

        }


    }
}
